/*
 * @(#) UnivRoleImpl.java
 *Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */
package in.ac.dei.edrp.cms.daoimpl.university;

import in.ac.dei.edrp.cms.dao.university.RoleConnect;
import in.ac.dei.edrp.cms.domain.university.UnivRoleInfoGetter;

import org.apache.log4j.Logger;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import java.util.List;
import java.util.StringTokenizer;


/**
 * This file consist of the methods used for setting up
 * the university role setup.
 * @author Ashish
 * @date 18 Jan 2011
 * @version 1.0
 */
public class UnivRoleImpl extends SqlMapClientDaoSupport implements RoleConnect {
    private Logger loggerObject = Logger.getLogger(UnivRoleImpl.class);

    /**
    * Method to get the list of roles defined in the university
    * @param userId userId of the logged in person
    * @param counter for developer in order to execute queries on choice
    * @return list of roles in the university
    */
    @SuppressWarnings("unchecked")
    public List<UnivRoleInfoGetter> getUniversityRoles(UnivRoleInfoGetter input) {
        List roleList;
        UnivRoleInfoGetter beanobject = new UnivRoleInfoGetter();

        String GROUP_CODE = "UNROLE";

        String uniId = input.getUserId().substring(1, 5);
        beanobject.setUniversityCode(uniId);
        beanobject.setGroupCode(GROUP_CODE);
        beanobject.setEmployeeCode(input.getEmployeeCode());

        try {
            if (input.getCounter().equalsIgnoreCase("one")) {
                roleList = (List) getSqlMapClientTemplate()
                                      .queryForList("unirolesetup.getComponentsInfo",
                        beanobject);

                return roleList;
            } else if (input.getCounter().equalsIgnoreCase("two")) {
                roleList = (List) getSqlMapClientTemplate()
                                      .queryForList("unirolesetup.getsetuniversityroles",
                        beanobject);

                return roleList;
            } else if (input.getCounter().equalsIgnoreCase("three")) {
                roleList = (List) getSqlMapClientTemplate()
                .queryForList("unirolesetup.getemployeeroles",
                		beanobject);               
                	
                	return roleList;
                	
}
        } catch (Exception e) {
            loggerObject.error("getUniversityRoles" + e);
        }

        return null;
    }

    /**
     * Method to get the list of links(menu items)
     * @param roleId roleId of the selected role
     * @param userId userId of the application user
     * @return list of links on the application for the role
     */
    @SuppressWarnings("unchecked")
    public List<UnivRoleInfoGetter> getMenuList(String roleId, String userId) {
        List menuList;

        UnivRoleInfoGetter roleObject = new UnivRoleInfoGetter();
        String uniId = userId.substring(1, 5);

        roleObject.setRoleId(roleId);
        roleObject.setUniversityCode(uniId);

        try {
            menuList = (List) getSqlMapClientTemplate()
                                  .queryForList("unirolesetup.getmenulist",
                    roleObject);

            return menuList;
        } catch (Exception e) {
            loggerObject.error("getmenulist" + e);
        }

        return null;
    }

    /**
     * Method to define role authorities
     * @param menus list of selected menu items
     * @param userId userId of the logged in person
     * @param roleId roleId selected from the interface
     * @return String
     */
    public String setRoleAuthorities(StringTokenizer menus, String userId,
        String roleId) {
        String uniId = userId.substring(1, 5);

        try {
            UnivRoleInfoGetter beanobject = new UnivRoleInfoGetter();

            while (menus.hasMoreTokens()) {
                beanobject.setMenuItemId(menus.nextToken());
                beanobject.setRoleId(roleId);
                beanobject.setAuthority(1);
                beanobject.setUniversityCode(uniId);
                beanobject.setCreatorId(userId);

                getSqlMapClientTemplate()
                    .insert("unirolesetup.setroleauthorities", beanobject);
            }
        } catch (Exception e) {
            loggerObject.error("setroleauthorities" + e);
        }

        return "success";
    }

    /**
     * Method to get the list of the menu items already defined for a role
     * @param userId userId of the logged in person
     * @param roleId roleId selected from the list
     * @return list of menu items
     */
    @SuppressWarnings("unchecked")
    public List<UnivRoleInfoGetter> getSetMenuList(String userId, String roleId) {
        List menuList;

        String uniId = userId.substring(1, 5);

        try {
            UnivRoleInfoGetter beanobject = new UnivRoleInfoGetter();

            beanobject.setUniversityCode(uniId);
            beanobject.setRoleId(roleId);

            menuList = (List) getSqlMapClientTemplate()
                                  .queryForList("unirolesetup.getsetmenulist",
                    beanobject);

            return menuList;
        } catch (Exception e) {
            loggerObject.error("getsetmenulist" + e);
            loggerObject.error("exception in getsetmenulist" + e);
        }

        return null;
    }

    /**
     * Method to delete menu items authorities for the selected role
     * @param univ list of selected menu items
     * @param userId userId of the logged in person
     * @param roleId roleId of the above role
     * @return String
     */
    public String deleteUniversityRolesDetails(StringTokenizer univ,
        String userId, String roleId) {
        String uniId = userId.substring(1, 5);
        UnivRoleInfoGetter beanobject = new UnivRoleInfoGetter();

        try {
            while (univ.hasMoreTokens()) {
                beanobject.setUniversityCode(uniId);
                beanobject.setMenuItemId(univ.nextToken());
                beanobject.setRoleId(roleId);

                getSqlMapClientTemplate()
                    .delete("unirolesetup.deletemenuitemauthority", beanobject);
            }

            return "success";
        } catch (Exception e) {
            loggerObject.error(" in delete setroleauthorities " + e);
        }

        return null;
    }
}
